package org.xs.web.util;

 
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import net.sf.sojo.interchange.json.JsonSerializer;

import org.apache.commons.lang.ArrayUtils;

public class Helpers {
	public static final String PERCEND_SIN="%s%%";
	public static String percent(String number){
		return String.format(PERCEND_SIN, number);
	}
	public static String cutStr(String str,int maxLength){
		int i=0;
		if(str.length() > maxLength){
			for(char c : str.toCharArray()){
				if(c  < 127){
					i++;
					if(i%3 == 0){
						maxLength++;
					}
				}
			}
			 
			maxLength = maxLength +  (i/7);	
			return str.substring(0,maxLength)+"...";
		}
		return str;	
	}
	
//	public static void selectedJSONArray(JSONArray jsonArray,String key,int value){
//		for(int i=0;i<jsonArray.size();i++){
//			JSONObject item = jsonArray.getJSONObject(i);
//			if(item.getInt(key) == value){
//				item.put("selected", "selected");
//				return ;
//			}
//		}
//	}
	
	
	public static Map<String, Object> createMap(String key,Object value){
		Map<String, Object> res = new HashMap<String, Object>();
		res.put(key, value);
		return res;
	}
	public static Map<String, Object> createMap(String key,Object value,int size){
		Map<String, Object> res = new HashMap<String, Object>(size);
		res.put(key, value);
		return res;
	}
	
//	
//	public static String getServPathAndPort(Request request){
//		if(request.getScheme().equals("http") && request.getServerPort() == 80){
//			return "http://"+request.getServerName()+ServletContexts.getContextpath();
//		}
//		if(request.getScheme().equals("https") && request.getServerPort() == 443){
//			return "https://"+request.getServerName()+ServletContexts.getContextpath();
//		}
//		return request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+ServletContexts.getContextpath();
//		
//	}
	
	public static int object2Int(Object intStr){
		if(intStr == null) return 0;
		return Integer.valueOf(String.valueOf(intStr));
	}
	
//	public static String getLocationByIP(String ip){
//		IPSeeker seeker = IPSeeker.getInstance();
//		return seeker.getCountry(ip);
//	}
	
	public static String encode(String str){
		try {
			return str == null ? null : URLEncoder.encode(str,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			return str;
		}
	}
	
	public static String decode(String str){
		try {
			return str == null ? null : URLDecoder.decode(str,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			return str;
		}
	}
	
	public static boolean isIpGtThat(String ip,String that){
		String[] ip_ = ip.split("\\.");
		String[] that_ = that.split("\\.");
		for(int i=0 ; i< 3 ;i++){
			if(Integer.valueOf(ip_[i]) > Integer.valueOf(that_[i])){
				return true;
			} 
		}
		return false;
	}
//	public static void servletWrite(HttpServletResponse out,WritableWorkbook book,String filename) throws IOException, WriteException{
//		out.setContentType("application/vnd.ms-excel");
//		String filenameEncode = ServletContexts.isIEMode() ? URLEncoder.encode(filename,"UTF-8") : MimeUtility.encodeText(filename, "UTF8", "B");;
//		out.setHeader("Content-disposition","attachment;filename=".concat(filenameEncode).concat(".xls"));
//		book.write();
//		book.close();
//	}
	
	public static Map<String, Object> copyMap(Map<String, Object> src){
		Map<String, Object> params = new HashMap<String, Object>();
		for(Map.Entry<String, Object> entry : src.entrySet()){
			if(entry.getValue().getClass().isArray()){
				params.put(entry.getKey(), ArrayUtils.clone((Object[])entry.getValue()));
			}else{
				params.put(entry.getKey(), entry.getValue());
			}			 
		}
		return params;
	}
	
	public static boolean inArray(Object[] arrays,Object value){
		for(Object i : arrays){
			if(i == value || i.equals(value)){
				return true;
			}
		}
		return false;
	}
	
	public static String dateTime(){
		return DateFormat.toDateString(new Date());
	}
	public static String dateTimeS(){
		return DateFormat.dateFormat(new Date());
	}
	public static Object[] arrayMerge(Object[] n1,Object[] n2){
		Object[] dest = new Object[n1.length + n2.length];
		System.arraycopy(n1, 0, dest, 0, n1.length);
		System.arraycopy(n2, 0, dest, n1.length, n2.length);
		
		return dest;
	}

	
	
	public static String toJson(Object object){
		JsonSerializer js = new JsonSerializer();
		return (String)js.serialize(object,new String[]{"class","~unique-id~"});
	}
	
	
	public static String join(Object[] args){
		StringBuilder sb = new StringBuilder();
		for(Object arg : args){
			sb.append(arg).append(",");
		}
		if(sb.length() > 0){
			return sb.substring(0,sb.length()-1);
		}
		return "";
	}
	
	public static int toInt(Object obj,int dflt){
		try {
			return Integer.parseInt((String)obj);
		} catch (Exception e) {
			return dflt;
		}
	}
	
	
}
